package com.example.administrator.myapplication.util.bitmap;

import android.graphics.Bitmap;
import android.util.LruCache;

import java.util.HashMap;

/**
 * 内存缓存工具类
 * Created by Administrator on 2016/4/13.
 */
public class MemoryCacheUtils {

    // private HashMap<String, SoftReference<Bitmap>> mMemroyCache = new
    // HashMap<String, SoftReference<Bitmap>>();
    // Android 2.3 (API Level
    // 9)开始，垃圾回收器会更倾向于回收持有软引用或弱引用的对象，这让软引用和弱引用变得不再可靠,建议用LruCache
    private final LruCache<String, Bitmap> mCache;
    private HashMap<String,Bitmap> mMemoryCache  = new HashMap<String,Bitmap>();


    public MemoryCacheUtils(){

        int maxMemory = (int) Runtime.getRuntime().maxMemory();

        mCache = new LruCache<String,Bitmap>(maxMemory/8){
            protected int sizeOf(String key, Bitmap value) {
                int size = value.getRowBytes() * value.getHeight();
                return size;
            }
        };

    }

    public Bitmap getBitmapFromMemory(String url) {
       /* Bitmap bitmap = mMemoryCache.get(url);
        return bitmap;*/
      return   mCache.get(url);
    }
    public void setBitmapToMemory(String url,Bitmap bitmap){
       // mMemoryCache.put(url, bitmap);
        mCache.put(url, bitmap);
    }
}
